package cn.itcast.blog.web.demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.itcast.blog.domain.AjaxResponse;
import cn.itcast.blog.domain.Category;

/**
 * servlet从3.0开始，支持注解方式配置servlet映射
 * @author 16604
 *
 */
@WebServlet(urlPatterns = "/table_data")
public class TableServlet extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		System.out.println(1);
		
		List<Category> list = new ArrayList<>();
		
		for(int i =0;i<10;i++) {
			Category c = new Category();
			
			c.setId(i);
			c.setName("分类"+i);
			c.setPriority(i);
			
			list.add(c);
		}
		
		AjaxResponse<Category> ajaxCategoryResponse = new AjaxResponse<>();
		ajaxCategoryResponse.setCode(0);
		ajaxCategoryResponse.setCount(100);//数据库一共有多少条信息，用于分页
		ajaxCategoryResponse.setMsg("");
		ajaxCategoryResponse.setData(list);
		
		String string = JSON.toJSONString(ajaxCategoryResponse);
		
		resp.setContentType("application/json;charset=utf-8");
		resp.getWriter().write(string);
	}
}
